package com.livesource.client.web.sourceCode.filesTree;

import java.util.Arrays;
import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.livesource.client.model.entry.ProjectClass;
import com.livesource.client.model.entry.ProjectFeature;
import com.livesource.client.utilities.FileExtensions;

public class FilesTree extends Tree {

	public FilesTree(String viewSelected, HashMap<String, ProjectClass> entries) {

		TreeItem root = new TreeItem("");

		if (entries != null) {

			TreeItem parent = root;

			String[] features = getSortedArray(entries);

			for (String feature : features) {

				ProjectFeature projectFeature = entries.get(feature);

				if (showFeature(viewSelected, projectFeature)) {

					String[] featurePath = feature.split("/");

					parent = root;

					for (int i = 0; i < featurePath.length - 1; i++) {

						String itemName = "<font size=1>" + "<img src='"
								+ GWT.getModuleName()
								+ "/images/folder-open.gif'> " + featurePath[i]
								+ "</font>";

						Integer childIndex = hasChild(parent, " "
								+ featurePath[i]);

						if (childIndex == null) {

							TreeItem node = new TreeItem(itemName);

							parent.addItem(node);

							node.setState(true);

							parent = node;

						} else {

							parent = parent.getChild(childIndex);
						}
					}

					TreeItem node = new TreeItem(new AnchorFileName(
							viewSelected, feature));

					parent.addItem(node);

					parent.setState(true);
				}
			}

			root.setState(true);

			openStates(root);

		}

		this.addItem(root);
	}

	private static void openStates(TreeItem item) {

		for (int i = 0; i < item.getChildCount(); i++) {

			item.getChild(i).setState(true);

			openStates(item.getChild(i));
		}
	}

	private static boolean showFeature(String viewSelected,
			ProjectFeature projectFeature) {

		if (FilesViewList.VIEW_FEATURES.equals(viewSelected)
				&& ProjectFeature.FEATURE_TYPE.equals(projectFeature
						.getFeatureType())) {

			return true;

		} else if (FilesViewList.VIEW_ARCHITECTURE.equals(viewSelected)
				&& ProjectFeature.ARCHITECTURE_TYPE.equals(projectFeature
						.getFeatureType())) {

			return true;

		} else if (FilesViewList.VIEW_ENTITIES.equals(viewSelected)
				&& ProjectFeature.ENTITY_TYPE.equals(projectFeature
						.getFeatureType())) {

			return true;

		} else if (FilesViewList.VIEW_USER_INTERFACE.equals(viewSelected)
				&& (ProjectFeature.FEATURE_TYPE.equals(projectFeature
						.getFeatureType()) || ProjectFeature.USER_INTERFACE_TYPE
						.equals(projectFeature.getFeatureType()))) {

			return true;

		} else if (FilesViewList.VIEW_CLASS_FILES.equals(viewSelected)
				&& FileExtensions
						.isClassFile(projectFeature.getFileExtension())) {

			return true;

		} else if (FilesViewList.VIEW_SOURCE_CODE.equals(viewSelected)) {

			return true;
		}

		return false;
	}

	private static Integer hasChild(TreeItem item, String itemName) {

		for (int j = 0; j < item.getChildCount(); j++) {

			if (item.getChild(j).getText().equals(itemName)) {

				return j;
			}
		}

		return null;
	}

	public static String[] getSortedArray(
			HashMap<String, ProjectClass> featuresMap) {

		String[] entries = new String[featuresMap.size()];

		featuresMap.keySet().toArray(entries);

		Arrays.sort(entries);

		return entries;
	}
}
